package cn.nextProject.controller;

import cn.nextProject.Utils.CommonUtils;
import cn.nextProject.configs.MinioConfig;
import cn.nextProject.entity.Result;
import cn.nextProject.entity.User;
import cn.nextProject.entity.req.FindToPage;
import cn.nextProject.service.UserService;
import cn.nextProject.service.minio.MinioService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private MinioService minioService;

    @Autowired
    private MinioConfig minioConfig;

    @PostMapping("/getUserListByUser")
    public Result<?> getUserListByUser(@RequestBody FindToPage<User> userFindToPage) {
        if (ObjectUtils.isEmpty(userFindToPage)){
            return new Result<>().fail("查询数据不能为空");
        }

        if (ObjectUtils.isEmpty(userFindToPage.getData())){
            return new Result<>().fail("搜索参数不能为空");
        }

        if (ObjectUtils.isEmpty(userFindToPage.getPageNum()) || ObjectUtils.isEmpty(userFindToPage.getPageSize())){
            return new Result<>().fail("分页参数不能为空");
        }

        PageInfo<User> byUser = userService.findByUser(userFindToPage.getData(), userFindToPage.getPageNum(), userFindToPage.getPageSize());

        return new Result<>().success("查询成功",byUser.getList(),byUser.getTotal());
    }

    @Transactional
    @PostMapping("/upLoadHeadPicture")
    public Result<?> upLoadHeadPicture(@RequestParam("file") MultipartFile file) throws Exception {
        String username = CommonUtils.getToken();
        if (ObjectUtils.isEmpty(username)){
            return new Result<>().fail("用户未登录！");
        }

        if (file.isEmpty()){
            return new Result<>().fail("上传数据不能为空");
        }

        User user = userService.getUserByUsername(username);

        if (ObjectUtils.isEmpty(user)){
            return new Result<>().fail("用户不存在");
        }

        String bucketName = minioConfig.getBucketName();
        boolean nextProject = minioService.bucketExists(bucketName);

        if (nextProject == false){
            //创建桶
            minioService.createBucket(bucketName);
        }

        //上传文件
        String uploadFile = minioService.uploadFile(bucketName, file);

        user.setHeadPicture(uploadFile);

        userService.updateUser(user);

        return new Result<>().success("上传成功",uploadFile);
    }

    @GetMapping("/findAllNoRoleGroup")
    public Result<?> findAllNoRoleGroup(@RequestParam("roleGroupId") Long roleGroupId) {
        if (ObjectUtils.isEmpty(roleGroupId)){
            return new Result<>().fail("参数不能为空");
        }

        List<User> allNoRoleGroup = userService.findAllNoRoleGroup(roleGroupId);

        return new Result<>().success("查询成功",allNoRoleGroup);
    }



}
